﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using CE.Database;
using CE.Security;

/// <summary>
/// Summary description for JobDB
/// </summary>
namespace CE.Job
{
    public class JobDB
    {
        public JobDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public Int64 InsertUpdate(Job job)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocJobInsertUpdateSingleItem";
            SortedList _sPara = new SortedList();
            #region Parameter
            _sPara.Add("@JobID", job.JobID);
            _sPara.Add("@JobTitle", job.JobTitle);
            _sPara.Add("@JobDescription", job.JobDescription);
            _sPara.Add("@Responsibilities", job.Responsibilities);
            _sPara.Add("@Requirements", job.Requirements);
            _sPara.Add("@StartDate", job.StartDate);
            _sPara.Add("@EndDate", job.EndDate);
            _sPara.Add("@Country", job.Country);
            _sPara.Add("@CreatedBy", job.CreatedBy);
            _sPara.Add("@Status", job.Status);
            _sPara.Add("@Info1", job.Info1);
            _sPara.Add("@Info2", job.Info2);
            _sPara.Add("@Info3", job.Info3);
            _sPara.Add("@Info4", job.Info4);
            _sPara.Add("@Info5", job.Info5);
            _sPara.Add("@Info6", job.Info6);
            _sPara.Add("@Info7", job.Info7);
            _sPara.Add("@Info8", job.Info8);
            _sPara.Add("@Info9", job.Info9);
            _sPara.Add("@Info10", job.Info10);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);
            return _result;
        }
        public Job GetJobByID(int JobID)
        {
            Job _job = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocJobSelectSingleItemByID]";

            SortedList _spara = new SortedList();
            _spara.Add("@JobID", JobID);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _job = new Job();
                    _job = FillDataRecord(_dt);
                }
            }
            return _job;
        }
        public Int64 DeleteJobByID(int JobID, Int64 HRID)
        {
            Int64 _result = -1;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocJobDeleteSingleItemByID]";

            SortedList _spara = new SortedList();
            _spara.Add("@JobID", JobID);
            _spara.Add("@DeletedBy", HRID);

            _result = _db.ExecuteSP(_spName, _spara);
            return _result;
        }
        private Job FillDataRecord(DataTable myDataTable)
        {
            Job myJob = new Job();
            myJob.JobID = Convert.ToInt64(myDataTable.Rows[0]["JobID"].ToString());
            myJob.JobTitle = myDataTable.Rows[0]["JobTitle"].ToString();
            myJob.JobDescription = myDataTable.Rows[0]["JobDescription"].ToString();
            myJob.Responsibilities = myDataTable.Rows[0]["Responsibilities"].ToString();
            myJob.Requirements = myDataTable.Rows[0]["Requirements"].ToString();
            myJob.StartDate = Convert.ToDateTime(myDataTable.Rows[0]["StartDate"].ToString());
            myJob.EndDate = Convert.ToDateTime(myDataTable.Rows[0]["EndDate"].ToString());
            myJob.Country = myDataTable.Rows[0]["Country"].ToString();
            myJob.CreatedBy = Convert.ToInt64(myDataTable.Rows[0]["CreatedBy"].ToString());
            myJob.Status = Convert.ToBoolean(myDataTable.Rows[0]["Status"].ToString());
            myJob.CreatedDate = Convert.ToDateTime(myDataTable.Rows[0]["CreatedDate"].ToString());
            myJob.ModifiedDate = Convert.ToDateTime(myDataTable.Rows[0]["ModifiedDate"].ToString());

            return myJob;
        }

        private Job FillDataRecord(IDataRecord myDataRecord)
        {
            Job myJob = new Job();
            myJob.JobID = myDataRecord.GetInt64(myDataRecord.GetOrdinal("JobID"));
            myJob.JobTitle = myDataRecord.GetString(myDataRecord.GetOrdinal("JobTitle"));
            myJob.JobDescription = myDataRecord.GetString(myDataRecord.GetOrdinal("JobDescription"));
            myJob.Responsibilities = myDataRecord.GetString(myDataRecord.GetOrdinal("Responsibilities"));
            myJob.Requirements = myDataRecord.GetString(myDataRecord.GetOrdinal("Requirements"));
            myJob.StartDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("StartEndDate"));
            myJob.EndDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("EndDate"));
            myJob.Country = myDataRecord.GetString(myDataRecord.GetOrdinal("Country"));
            myJob.CreatedBy = myDataRecord.GetInt64(myDataRecord.GetOrdinal("CreatedBy"));
            myJob.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            myJob.CreatedDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDate"));
            myJob.ModifiedDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDate"));
            return myJob;
        }
    }
}